#tag Class
Protected Class sjgNumField
Inherits EditField
	#tag Event
		Function KeyDown(Key As String) As Boolean
		  if InStr(0,mValidChars,key) = 0 then
		    Beep
		    Return True
		  end if
		  Dim s As String = me.Text
		  dim PreString,PostString as String
		  dim selEnd As Integer = me.SelStart + me.SelLength
		  PreString = Left(s,me.SelStart)
		  PostString = Mid(s,selEnd+1)
		  if InStr(0,kPrintableChars,key) <> 0 then
		    s = PreString + Key + PostString
		    If s <> "-" and s <> "+" and s <> "." and isnumeric(s) = False Then
		      Beep
		      Return True
		    End If
		  end if
		  
		End Function
	#tag EndEvent

	#tag Event
		Sub LostFocus()
		  Dim v As Double
		  
		  If IsNumeric(me.Text) = false then
		    RaiseEvent InvalidEntry
		  else
		    v = val(me.Text)
		    If v < mMinValue or v > mMaxValue Then
		      RaiseEvent InvalidEntry
		    Else
		      me.Text = Format(v,mFormat)
		    End If
		  End If
		End Sub
	#tag EndEvent

	#tag Event
		Sub Open()
		  setValidChars
		End Sub
	#tag EndEvent

	#tag Event
		Sub ValidationError(InvalidText As String, StartPosition As Integer)
		  //Implemented to avoid confusion
		End Sub
	#tag EndEvent


	#tag Method, Flags = &h21
		Private Sub setValidChars()
		  mValidChars = "0123456789"
		  if DecimalPlaces > 0 then
		    mValidChars = mValidChars + "."
		  end if
		  If minValue < 0 Then
		    mValidChars = mValidChars + "-"
		  End If
		  If maxValue >= 0 Then
		    mValidChars = mValidChars + "+"
		  End If
		  mValidChars = mValidChars + chr(8) + chr(127) + chr(9)
		  mValidChars = mValidChars + chr(28) + chr(29) + chr(30) + chr(31)
		  Dim i As Integer
		  mFormat  = "-0"
		  If DecimalPlaces > 0 Then
		    mFormat = mFormat + "."
		    For i = 1 to DecimalPlaces
		      mFormat = mFormat + "0"
		    Next
		  End If
		End Sub
	#tag EndMethod


	#tag Hook, Flags = &h0
		Event InvalidEntry()
	#tag EndHook


	#tag Note, Name = by Steve Garman
		
		This EditField subclass is provided to try to avoid the confusion people seem to find
		when trying to use EditField.Mask to force numeric input
	#tag EndNote

	#tag Note, Name = Usage
		Place this class in your project
		
		Drag an EditField onto your window and change its Super to
		sjgNumField
		
		In the Properties pane, set appropriate values for 
		DecimalPlaces
		   (0 = Integer, 2 for Dollars & Cents etc.)
		MinValue
		  (e.g. -1000.00)
		MaxValue
		  (e.g. 1000.00) 
		
		If you wish to allow minus signs(-), then MinValue must be negative.
		
		If you wish to allow plus signs(+), then MaxValue must not be negative.
		
		//------------------------------------------------------------------------------------
		
		
		If the user enters an invalid character into the string, the keyDown event attempts
		to catch it and refuse to accept it.
		
		When the control loses focus
		1) If the entry is valid, it is reformatted to a simple numeric mask based on the DecimalPlaces
		2) If the entry is invalid,the InvaldEntry event fires. It is the programmer's responsibility to take
		    the appropriate action
	#tag EndNote


	#tag Property, Flags = &h21
		Private mMinValue As Double
	#tag EndProperty

	#tag Property, Flags = &h21
		Private mMaxValue As Double
	#tag EndProperty

	#tag Property, Flags = &h21
		Private mValidChars As String
	#tag EndProperty

	#tag Property, Flags = &h21
		Private mDecimalPlaces As Integer
	#tag EndProperty

	#tag ComputedProperty, Flags = &h0
		#tag Getter
			Get
			  return mDecimalPlaces
			End Get
		#tag EndGetter
		#tag Setter
			Set
			  mDecimalPlaces = value
			  setValidChars
			End Set
		#tag EndSetter
		DecimalPlaces As Integer
	#tag EndComputedProperty

	#tag ComputedProperty, Flags = &h0
		#tag Getter
			Get
			  return mminValue
			End Get
		#tag EndGetter
		#tag Setter
			Set
			  mminValue = value
			  setValidChars
			End Set
		#tag EndSetter
		MinValue As Double
	#tag EndComputedProperty

	#tag ComputedProperty, Flags = &h0
		#tag Getter
			Get
			  return mmaxValue
			End Get
		#tag EndGetter
		#tag Setter
			Set
			  mmaxValue = value
			  setValidChars
			End Set
		#tag EndSetter
		MaxValue As Double
	#tag EndComputedProperty

	#tag Property, Flags = &h21
		Private mFormat As String
	#tag EndProperty


	#tag Constant, Name = kPrintableChars, Type = String, Dynamic = False, Default = \"0123456789+-.", Scope = Public
	#tag EndConstant


	#tag ViewBehavior
		#tag ViewProperty
			Name="Name"
			Visible=true
			Group="ID"
			Type="String"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Index"
			Visible=true
			Group="ID"
			Type="Integer"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Super"
			Visible=true
			Group="ID"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Left"
			Visible=true
			Group="Position"
			Type="Integer"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Top"
			Visible=true
			Group="Position"
			Type="Integer"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Width"
			Visible=true
			Group="Position"
			InitialValue="80"
			Type="Integer"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Height"
			Visible=true
			Group="Position"
			InitialValue="22"
			Type="Integer"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="LockLeft"
			Visible=true
			Group="Position"
			Type="Boolean"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="LockTop"
			Visible=true
			Group="Position"
			Type="Boolean"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="LockRight"
			Visible=true
			Group="Position"
			Type="Boolean"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="LockBottom"
			Visible=true
			Group="Position"
			Type="Boolean"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="TabPanelIndex"
			Group="Position"
			InitialValue="0"
			Type="Integer"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="TabIndex"
			Visible=true
			Group="Position"
			InitialValue="0"
			Type="Integer"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="TabStop"
			Visible=true
			Group="Position"
			InitialValue="True"
			Type="Boolean"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Border"
			Visible=true
			Group="Appearance"
			InitialValue="True"
			Type="Boolean"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Multiline"
			Visible=true
			Group="Appearance"
			Type="Boolean"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="ScrollbarHorizontal"
			Visible=true
			Group="Appearance"
			Type="Boolean"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="ScrollbarVertical"
			Visible=true
			Group="Appearance"
			InitialValue="True"
			Type="Boolean"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Styled"
			Visible=true
			Group="Appearance"
			Type="Boolean"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Password"
			Visible=true
			Group="Appearance"
			Type="Boolean"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="UseFocusRing"
			Visible=true
			Group="Appearance"
			InitialValue="True"
			Type="Boolean"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="TextColor"
			Visible=true
			Group="Appearance"
			InitialValue="&h000000"
			Type="Color"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="BackColor"
			Visible=true
			Group="Appearance"
			InitialValue="&hFFFFFF"
			Type="Color"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Enabled"
			Visible=true
			Group="Appearance"
			InitialValue="True"
			Type="Boolean"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Format"
			Visible=true
			Group="Appearance"
			Type="String"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Visible"
			Visible=true
			Group="Appearance"
			InitialValue="True"
			Type="Boolean"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="HelpTag"
			Visible=true
			Group="Appearance"
			Type="String"
			EditorType="MultiLineEditor"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="AutoDeactivate"
			Visible=true
			Group="Appearance"
			InitialValue="True"
			Type="Boolean"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="TextFont"
			Visible=true
			Group="Font"
			InitialValue="System"
			Type="String"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="TextSize"
			Visible=true
			Group="Font"
			InitialValue="0"
			Type="Integer"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Bold"
			Visible=true
			Group="Font"
			Type="Boolean"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Italic"
			Visible=true
			Group="Font"
			Type="Boolean"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Underline"
			Visible=true
			Group="Font"
			Type="Boolean"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Text"
			Visible=true
			Group="Initial State"
			Type="String"
			EditorType="MultiLineEditor"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Mask"
			Visible=true
			Group="Behavior"
			Type="String"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="ReadOnly"
			Visible=true
			Group="Behavior"
			Type="Boolean"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="LimitText"
			Visible=true
			Group="Behavior"
			InitialValue="0"
			Type="Integer"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="AcceptTabs"
			Visible=true
			Group="Behavior"
			Type="Boolean"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Alignment"
			Visible=true
			Group="Behavior"
			InitialValue="0"
			Type="Integer"
			EditorType="Enum"
			InheritedFrom="EditField"
			#tag EnumValues
				"0 - Default"
				"1 - Left"
				"2 - Center"
				"3 - Right"
			#tag EndEnumValues
		#tag EndViewProperty
		#tag ViewProperty
			Name="DataSource"
			Visible=true
			Group="Database Binding"
			Type="String"
			EditorType="DataSource"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="DataField"
			Visible=true
			Group="Database Binding"
			Type="String"
			EditorType="DataField"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="InitialParent"
			InheritedFrom="EditField"
		#tag EndViewProperty
		#tag ViewProperty
			Name="DecimalPlaces"
			Visible=true
			Group="Behavior"
			InitialValue="0"
			Type="Integer"
		#tag EndViewProperty
		#tag ViewProperty
			Name="minValue"
			Visible=true
			Group="Behavior"
			InitialValue="0"
			Type="Double"
		#tag EndViewProperty
		#tag ViewProperty
			Name="maxValue"
			Visible=true
			Group="Behavior"
			InitialValue="0"
			Type="Double"
		#tag EndViewProperty
	#tag EndViewBehavior
End Class
#tag EndClass
